/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmPartPolXProcEmp.java
 *
 * Created on 02-jun-2011, 2:34:14
 */
package verins.GUI;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.table.DefaultTableModel;
import verins.BO.PartidxProcesoElecBO;
import verins.Utils.Mensajes;
import verins.beans.Partidopolitico;
import verins.beans.Partidopoliticoxprocesoelectoral;
import verins.beans.Procesoelectoral;
import verins.beans.Procesoempadronamiento;
import verins.beans.Tipopartidopolitico;

/**
 *
 * @author hectdx
 */
public class frmPartPolXProcEmp extends JDialog {
    private Procesoelectoral procesoEle=null;
    private List lstPartidos=new ArrayList();
    public Partidopolitico partido=null;
    int cantPartIni=0;
    
    /** Creates new form frmPartPolXProcEmp */
    public frmPartPolXProcEmp(Procesoempadronamiento procesoEmpa, JDialog vent) {
        super(vent,true);
        procesoEle=procesoEmpa.getIdprocesoelectoral();
        initComponents();
        verins.Utils.Comunes.centrarVentana(this);
        llenarTabla();
        lnkRepresentantes.setVisible(true);
        if(procesoEle!=null){
            lblProcElect.setText(procesoEle.getNombre().toUpperCase());
            lblProcEmpad.setText(procesoEmpa.getDescripcion().toUpperCase());
            cantPartIni=procesoEle.getPartidopoliticoxprocesoelectoralList().size();
        }
        lnkRepresentantes.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblProcElect = new javax.swing.JLabel();
        lblProcEmpad = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblPartidos = new javax.swing.JTable();
        btnAdd = new javax.swing.JButton();
        btnDrop = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        lnkRepresentantes = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Partidos participantes");
        setFocusable(false);
        setResizable(false);

        lblProcElect.setFont(new java.awt.Font("Ubuntu", 1, 15));
        lblProcElect.setText("proc Elect");

        lblProcEmpad.setFont(new java.awt.Font("Ubuntu", 1, 15));
        lblProcEmpad.setText("Proced Empad");

        tblPartidos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Partido Politico", "Estado", "Fecha Inscripción"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblPartidos);
        tblPartidos.getColumnModel().getColumn(0).setResizable(false);
        tblPartidos.getColumnModel().getColumn(1).setResizable(false);
        tblPartidos.getColumnModel().getColumn(2).setResizable(false);

        btnAdd.setFont(new java.awt.Font("Ubuntu", 1, 10));
        btnAdd.setText("+");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnDrop.setFont(new java.awt.Font("Ubuntu", 1, 10));
        btnDrop.setText("-");
        btnDrop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDropActionPerformed(evt);
            }
        });

        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        lnkRepresentantes.setBackground(javax.swing.UIManager.getDefaults().getColor("Desktop.background"));
        lnkRepresentantes.setForeground(java.awt.Color.blue);
        lnkRepresentantes.setText("Asociar Representantes");
        lnkRepresentantes.setBorderPainted(false);
        lnkRepresentantes.setContentAreaFilled(false);
        lnkRepresentantes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lnkRepresentantesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblProcElect)
                    .addComponent(lblProcEmpad)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 567, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnDrop, 0, 0, Short.MAX_VALUE)
                            .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lnkRepresentantes, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 209, Short.MAX_VALUE)
                        .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblProcElect)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblProcEmpad)
                        .addGap(45, 45, 45)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)
                        .addGap(18, 18, 18))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(131, 131, 131)
                        .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnDrop, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(lnkRepresentantes, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnGuardar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    
    public void llenarTabla(){
        SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
        lstPartidos= procesoEle.getPartidopoliticoxprocesoelectoralList();//PartidxProcesoElecBO.findAll(1);//obtener lista
        verins.Utils.Comunes.limpiarTabla(tblPartidos);
        DefaultTableModel dtm=(DefaultTableModel)tblPartidos.getModel();
        if (lstPartidos!=null&& lstPartidos.size()>0){
            Iterator it=lstPartidos.iterator();
            Partidopoliticoxprocesoelectoral p=null;
            while(it.hasNext()){
                p=(Partidopoliticoxprocesoelectoral)it.next();
                Object fila[]=new Object[] {p.getPartidopolitico().getNombre(), 
                                            p.getEstadoinscrip(),
                                            p.getFechainscrip()!=null?sdf.format(p.getFechainscrip()) :"-/-/-",
                                            };
                dtm.addRow(fila);
            }
        }
        verins.Utils.Comunes.setearColumnas(tblPartidos);
    }
    
    
   public void mostrarBotonesGrilla(boolean b){
         btnAdd.setVisible(b);
         btnDrop.setVisible(b);
    }
    
    public void setearBotonSalirCancelar(String texto){
        btnCancelar.setText(texto);
    }
    
    public void setearBotonGuardarEditar(String texto){
        btnGuardar.setText(texto);
    }
    
     private Partidopolitico obtenerPartidoSelecc(){
         if (lstPartidos!=null){
            int fila=tblPartidos.getSelectedRow();
                if (fila>=0){
                    partido=((Partidopoliticoxprocesoelectoral)lstPartidos.get(fila)).getPartidopolitico();
                } else 
                    partido= null;
         }
           return partido;
    }
    
    private void lnkRepresentantesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lnkRepresentantesActionPerformed
   
        // Procesoelectoral proceso,Partidopolitico pa, JDialog ventana
        obtenerPartidoSelecc();
        
        if (partido!=null){
            frmRepresentantesXPartido frm=new frmRepresentantesXPartido(procesoEle,partido,this);
            frm.setVisible(true);
        }else{
            verins.Utils.Mensajes.alerta("Debe elegir un partido");
        }
}//GEN-LAST:event_lnkRepresentantesActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        frmBusquedaPartidoPolitico frmBusq=new frmBusquedaPartidoPolitico(null,this);
        frmBusq.setearBotonesBusqueda();
        int tipoProc=procesoEle.getIdtipoprocesoelectoral().getIdtipoprocesoelectoral();
        frmBusq.seteaTipoPartido(new Tipopartidopolitico(tipoProc));
        frmBusq.seteaPartidoActivo();
        frmBusq.setVisible(true);
        partido=frmBusq.partidoPolitico;
        
        if (partido!=null){
           if (!esRepetido(lstPartidos,partido)) {
               DefaultTableModel dtm=(DefaultTableModel)tblPartidos.getModel();
               Object fila[]=new Object[] {partido.getNombre(),
                                           verins.Utils.Constantes.EN_INSCRIPCION,"-/-/-"};
               dtm.addRow(fila);
               //lista para grabar
               Partidopoliticoxprocesoelectoral pxp=new Partidopoliticoxprocesoelectoral(partido.getIdpartidopolitico(), procesoEle.getIdprocesoelectoral());
               pxp.setEstadoinscrip(verins.Utils.Constantes.EN_INSCRIPCION);
               pxp.setPartidopolitico(partido);
               lstPartidos.add(pxp);
               partido=null;
           }else{
               Mensajes.alerta("Partido ya registrado");
           }
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        dispose();
        
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        //verificar 
        if (((JButton)evt.getSource()).getText().equalsIgnoreCase("guardar")){
            if (verins.Utils.Mensajes.guardar(evt, this)){
                if (verins.Utils.Mensajes.exito(PartidxProcesoElecBO.guardarPartidos(lstPartidos)))
                dispose();
            }
        }else{//
            mostrarBotonesGrilla(true);
            lnkRepresentantes.setVisible(true);
            setearBotonGuardarEditar("Guardar");
            setearBotonSalirCancelar("Cancelar");
        }
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void btnDropActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDropActionPerformed
        // TODO add your handling code here:
        
         if (lstPartidos!=null){
            int fila=tblPartidos.getSelectedRow();
            if (fila>=0){
                Partidopoliticoxprocesoelectoral p=(Partidopoliticoxprocesoelectoral)lstPartidos.get(tblPartidos.getSelectedRow());
                if (fila<cantPartIni){
                    verins.Utils.Mensajes.alerta("El partido no puede ser removido");
                }else{
                    lstPartidos.remove(fila);
                   llenarTabla();
                }
            }
        }
    }//GEN-LAST:event_btnDropActionPerformed

     private boolean esRepetido(List lstPartidos,Partidopolitico partidoN){
         //System.out.println("in:" + partidoN.getIdpartidopolitico());
         Partidopoliticoxprocesoelectoral p=null;
        for(int j=0;j<lstPartidos.size();j++){
            p=(Partidopoliticoxprocesoelectoral)lstPartidos.get(j);
            //System.out.println(j+": "+p.getPartidopolitico());
            if (partidoN.getIdpartidopolitico()==p.getPartidopolitico().getIdpartidopolitico()) return true;
        }
        return false;
    }
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnDrop;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblProcElect;
    private javax.swing.JLabel lblProcEmpad;
    private javax.swing.JButton lnkRepresentantes;
    private javax.swing.JTable tblPartidos;
    // End of variables declaration//GEN-END:variables
}
